/*
* @Author: lenovo
* @Date:   2017-11-13 19:15:58
* @Last Modified by:   lenovo
* @Last Modified time: 2017-11-28 15:18:40
*/
const webpack = require('webpack');
var ExtractTextPlugin   = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin   = require('html-webpack-plugin');

// 环境变量配置，dev / online
var WEBPACK_ENV         = process.env.WEBPACK_ENV || 'dev';

// 获取html-webpack-plugin参数的方法 
var getHtmlConfig = function(name, title){
    return {
        template    : './src/view/' + name + '.html',
        filename    : 'view/' + name + '.html',
        title       : title,
        inject      : true,
        hash        : true,
        chunks      : ['common', name]
    };
};
var config= {
  entry: {
    'common': ['./src/page/common/index.js'],
  	'index': ['./src/page/index/index.js'],
    'user-login': ['./src/page/user-login/index.js'],
    'main': ['./src/page/common/main/index.js'],
    'content-item-meitu': ['./src/page/content-item-meitu/index.js'],
    'content-item-meiwen': ['./src/page/content-item-meiwen/index.js'],
    'content-item-music': ['./src/page/content-item-music/index.js'],
  	'content-item-video': ['./src/page/content-item-video/index.js']
  },
 output: {  
 	//打包到相应类型文件夹下
        path: './dist',
        publicPath : '/dist',
        filename: 'js/[name].js'
  },
  module: {
        loaders: [
            { test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader","css-loader")},
            { test: /\.(gif|png|jpg|woff|svg|eot|ttf|mp3)\??.*$/, loader: 'url-loader?limit=100&name=resource/[name].[ext]' },
            { test: /\.string$/, loader: 'html-loader'},
            { test: /\.(webm|mp4)$/, loader: "file-loader"}
        ]
   },
   plugins: [
        // 独立通用模块到js/base.js
        new webpack.optimize.CommonsChunkPlugin({
            name : 'common',
            filename : 'js/common.js'
        }),
         // 把css单独打包到文件里
        new ExtractTextPlugin("css/[name].css"),
        // html模板的处理
        new HtmlWebpackPlugin(getHtmlConfig('index', '首页')),
        new HtmlWebpackPlugin(getHtmlConfig('user-login', '用户登录')),
        new HtmlWebpackPlugin(getHtmlConfig('content-item-meitu', '美图~')),
        new HtmlWebpackPlugin(getHtmlConfig('content-item-meiwen', '美文~')),
        new HtmlWebpackPlugin(getHtmlConfig('content-item-music', '音乐~')),
        new HtmlWebpackPlugin(getHtmlConfig('content-item-video', '视频~')),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery"
        })
    ]
}
if('dev' === WEBPACK_ENV){
    config.entry.common.push('webpack-dev-server/client?http://localhost:8088/dist/view/index.html');
}

module.exports = config;
